apiready = function(){
seajs.use(['../../V',"../../api","../../jquery","../../app","../../service"],function(V,$api,$,app,service){
	var vm = {
		el: '.group-box',
	  	data: {
	    	endTime: '2018-06-29 00:00:00',
	    	timeList: '',
	    	timeMs: 9,
				groupInfo: {}, // 拼团信息
				orderId: null, // 订单id
				showDio: false, // 是否显示弹窗
				isMaster: false, // 是否为发起人
			name: '' //--窗口名
	  	},
	  	methods:{
				init: function () {
					this.orderId = api.pageParam.orderId;
					// this.orderId = '2018073115354110054495';

					var that = this;
					this.getGroupInfo(function () {
						if (that.isMaster) {
							that.initTimer();
						}
					});
				},

				// 获取拼团信息
				getGroupInfo: function (callback) {
					var that = this;
					if (!this.orderId) {
						api.toast({
						    msg: '获取订单失败',
						    duration: 2000,
						    location: 'bottom'
						});
						return;
					}
					// unionid: 'd3gyNWM5YjkwMWNjMzhjNDI4MTcyMzA1Mw=='
					app.ajax(
						service['getGroupDetail'],
						{
							data: {
								values: {
									order_sn: that.orderId,
									unionid: $api.getStorage("shop_unionid")
								}
							}
						},
						function (res) {
							if (res.code == 0) {
								that.groupInfo = res.data;
								that.endTime = res.data.expire_time.toString().replace(/-/g,"/");
								if (res.data.leader) {
									that.isMaster = true;
								} else {
									that.isMaster = false;
								}
								callback();
							} else {
								api.toast({
								    msg: '获取信息失败',
								    duration: 2000,
								    location: 'middle'
								});
							}
						}
					);
				},

				// 邀请好友拼团
				inviteFriend: function () {
					this.showDio = true;
				},

				// 获取分享链接
				getShareLink: function (callback) {
					var that = this;
					app.ajax(
						service['Share'],
						{
							data: {
								values: {
									type: 3,
									unionid: $api.getStorage("shop_unionid"),
									share_id: that.groupInfo.group_id
								}
							}
						},
						function (res) {
							if (res.code == 0) {
								var url = res.data;
								callback(url);
							} else {
								api.toast({
								    msg: '获取分享链接失败',
								    duration: 2000,
								    location: 'bottom'
								});
							}
						}
					);
				},

				// 分享到微信
				sharToWx: function (type) {
					var that = this;
					var wx = api.require('wx');
					var toWhere;
					if (type == 1) {
						// 分享给朋友
						toWhere = 'session';
					} else if (type == 2) {
						// 分享到朋友圈
						toWhere = 'timeline';
					}
					that.getShareLink(function(url) {
						wx.shareWebpage({
						    scene: toWhere,
						    title: that.groupInfo.name,
						    description: '您的好友向您分享了一个商品，打开连接即可查看',
						    contentUrl: url
						}, function(ret, err) {
						    if (ret.status) {
						        api.toast({
						            msg: '分享成功',
						            duration: 2000,
						            location: 'bottom'
						        });
						    } else {
									api.toast({
											msg: '分享失败',
											duration: 2000,
											location: 'bottom'
									});
						    }
						});
					});
				},

				// 返回店铺首页
				backHome: function () {
					var shopData = this.groupInfo.shop;
					var shop_user_id = this.groupInfo.shop.shop_user_id;
					// app.openWin('buyMain', {shopData: shopData, otherUserInfoId: shop_user_id});
					api.closeToWin({
					    name: 'shopIndex__'
					});

				},
				// 前往订单详情
				toOrderDetail: function () {
					var that = this;
					app.openWin('buyOrderDetail', {order_id: that.orderId});
				},

				// 关闭关闭
				closeDio: function () {
					this.showDio = false;
				},

				// 初始化倒计时
	  		initTimer: function () {
					var self = this;
		  		var timer = setInterval(function(){
		  			var sumTime = (new Date(self.endTime)) - (new Date());
		  			var h = 0, s = 0, m = 0, ms = 9;
		  			if(sumTime > 0){
		  				h = Math.floor(sumTime/(1000*60*60));
		  				s = Math.floor(sumTime%(1000*60*60)/(1000*60));
		  				m = Math.floor((sumTime%(1000*60*60)%(1000*60))/1000);
		  			}
		  			h = h > 10 ? h.toString() : "0"+h;
		  			s = s > 10 ? s.toString() : "0"+s;
		  			m = m > 10 ? m.toString() : "0"+m;
		  			var time = h+s+m;
		  			self.timeList = time.split("");
		  			var msTime = setInterval(function(){
		  				self.timeMs = ms;
		  				ms--;
		  				if(ms < 0){
		  					ms = 9;
		  				}
		  			},100);
		  			if(time == '000000'){
		  				clearInterval(timer);
		  				clearInterval(msTime);
		  				self.timeMs = 0;
		  			}
		  		},1000);
				}
	  	},
	  	mounted: function(){
	  		var self = this;
	  		function btnKeyBack(){
	  			api.sendEvent({
					name:'payBackGoods_'
				});
				$api.setTimeout(function(){
					api.closeToWin({
						name: self.name
					});
				},200);
	  		}
			api.addEventListener({
	  			name: 'keyback'
	  		},function(){
	  			btnKeyBack();
	  		});

	  		//--点击上面按钮返回
			$api.addEvt($api.dom('#btn-back-new'), 'click', function() {
	            btnKeyBack();
	        });

	        //--右滑
	        api.addEventListener({
			    name:'swiperight'
			},function(ret,err){
			    btnKeyBack();
         	});
	  	},
	  	created: function(){
	  		this.name = api.pageParam.name || api.pageParam.prve;
	  		this.init();
	  	}
	};
	var even = function() {}
	var newVue = new Vue(vm);
	even();
});
}
